package com.yimi.yinhepda.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.response.DifferTaskResponse;
import java.util.List;

/**
 * @author 000432
 */
public class DifferTaskAdapter extends BaseAdapter {

  private List<DifferTaskResponse.DifferTaskEntity> mDataList;
  private Activity mContext;

  public DifferTaskAdapter(Activity context, List<DifferTaskResponse.DifferTaskEntity> datas) {
    super();
    this.mContext = context;
    this.mDataList = datas;
  }

  /**
   * 获取当前位置信息
   */
  public int getCurrentPosition() {
    //设置其他的不选中
    for (int i = 0; i < mDataList.size(); i++) {
      if (mDataList.get(i).isChecked()) {
        return i;
      }
    }
    return -1;
  }

  public List<DifferTaskResponse.DifferTaskEntity> getDataList() {
    return mDataList;
  }

  public void setDataList(List<DifferTaskResponse.DifferTaskEntity> dataList) {
    mDataList = dataList;
  }

  @Override public int getCount() {
    return mDataList.size();
  }

  @Override public Object getItem(int position) {
    return mDataList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_differ_task_list_item, null);
      holder = new ViewHolder();
      holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.check_task_list_item);
      holder.differ_no = (TextView) convertView.findViewById(R.id.diff_bill_no);
      holder.missingNum = (TextView) convertView.findViewById(R.id.missing);
      holder.district = (TextView) convertView.findViewById(R.id.area);
      holder.time = (TextView) convertView.findViewById(R.id.time);
      holder.empName = (TextView) convertView.findViewById(R.id.empName);
      holder.radioButton = (RadioButton) convertView.findViewById(R.id.check_task_radio_button);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    DifferTaskResponse.DifferTaskEntity mItem = mDataList.get(position);
    //编号
    holder.differ_no.setText(mItem.getDiffreportNo());
    //少票
    holder.missingNum.setText(mItem.getLessNum() + "");
    //库区
    holder.district.setText(mItem.getStockArea());
    //差异时间
    holder.time.setText(mItem.getDifferTimeFormat(mItem.getDiffreportCreateTime()));
    //盘点人
    holder.empName.setText(mItem.getStockUser());

    //设置是否选中
    if (mDataList.get(position).isChecked()) {
      holder.radioButton.setChecked(true);
    } else {
      holder.radioButton.setChecked(false);
    }

    holder.linearLayout.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View arg0) {
        //设置其他的不选中
        for (DifferTaskResponse.DifferTaskEntity item : mDataList) {
          item.setChecked(false);
        }
        mDataList.get(position).setChecked(true);
        notifyDataSetChanged();
      }
    });

    //勾选
    holder.radioButton.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View view) {
        for (DifferTaskResponse.DifferTaskEntity item : mDataList) {
          item.setChecked(false);
        }
        mDataList.get(position).setChecked(true);
        notifyDataSetChanged();
      }
    });
    return convertView;
  }

  private static class ViewHolder {
    LinearLayout linearLayout;
    /** 编号 */
    TextView differ_no;
    /** 少票 */
    TextView missingNum;
    /** 库区 */
    TextView district;
    /** 差异时间 */
    TextView time;
    /** 盘点人 */
    TextView empName;
    /** 状态 */
    RadioButton radioButton;
  }
}
